package com.xy.job.executor.log.factory

import ch.qos.logback.classic.LoggerContext
import ch.qos.logback.classic.joran.JoranConfigurator
import ch.qos.logback.classic.util.LogbackMDCAdapter
import ch.qos.logback.core.util.StatusPrinter
import com.xy.job.executor.log.logger.LogbackLogger
import com.xy.job.executor.log.logger.Logger
import java.net.URL

internal class LogbackLoggerFactory : ILoggerFactory {

    private var loggerContext: LoggerContext = LoggerContext()

    init {
        loggerContext.setName("xy-job")
        loggerContext.mdcAdapter = LogbackMDCAdapter()
        val configurator = JoranConfigurator()
        configurator.context = loggerContext
        val url: URL = Log4j2LoggerFactory::class.java.getResource("/logback-xy-job.xml")!!
        configurator.doConfigure(url)
        StatusPrinter.print(loggerContext)
    }

    override fun getLogger(name: String?): Logger {
        return LogbackLogger(loggerContext.getLogger(name))
    }

}